{smcl}
{com}{sf}{ul off}{txt}{.-}
      name:  {res}<unnamed>
       {txt}log:  {res}C:\Users\myogo\Princeton Dropbox\Motohiro Yogo\Koijen Yogo - International\JPE\Replication\Code\1 Data\IMF_CPIS.smcl
  {txt}log type:  {res}smcl
 {txt}opened on:  {res}31 Oct 2025, 09:18:57
{txt}
{com}. /* Define local and global variables */
> 
> local directory = "../../Data/IMF/CPIS";
{txt}
{com}. do global;
{txt}
{com}. /* global.do (STATA)
>         Define global variables in "Data".
>         by Ralph Koijen & Motohiro Yogo */
. 
. #delimit ;
{txt}delimiter now ;
{com}. clear all;
{res}{txt}
{com}. global year_min = 2003;
{txt}
{com}. global year_max = 2020;
{txt}
{com}. global small = 5e-4;
{txt}
{com}.         /* Minimum reportable threshold for CPIS */

{txt}end of do-file

{com}. /* Step 1: Prepare countries */
> 
> u Counterpart Country_Code using Countries, clear;
{txt}
{com}. /* Rename variables */
> 
> rename Country_Code Counterpart_Country_Code;
{res}{txt}
{com}. /* Save data */
> 
> tempfile Countries;
{txt}
{com}. save `Countries';
{txt}{p 0 4 2}
file {bf}
C:\Users\myogo\AppData\Local\Temp\ST_9840_000001.tmp{rm}
saved
as .dta format
{p_end}

{com}. /* Step 2: Construct IMF CPIS bilateral positions */
> 
> /* Load data */
> 
> u Country_Code Indicator_Code Counterpart_Country_Code Counterpart_Sector_Code Sector_Code Attribute
>         _$year_min-_$year_max
>         if Country_Code!=Counterpart_Country_Code
>         & regexm(Indicator_Code,"^I_A_(D_L|D_S|E_T)_T")
>         & !inlist(Counterpart_Country_Code,1,31)        /* 1 = World, 31 = World Minus 25 Significant Financial Centers */
>         & Counterpart_Sector_Code=="T"
>         & Sector_Code=="T"
>         using "`directory'/CPIS_03-10-2022", clear;
{txt}
{com}. drop Counterpart_Sector_Code Sector_Code;
{txt}
{com}. /* Code confidential holdings */
> 
> sort Country_Code Indicator_Code Counterpart_Country_Code Attribute;
{txt}
{com}. foreach var of varlist _2011 _2012 _2014-_2019 {c -(};
{txt}  2{com}.         by Country_Code Indicator_Code Counterpart_Country_Code: replace `var' = ".c" if `var'[_n-1]=="C";
{txt}  3{com}. {c )-};
{txt}(1,089 real changes made)
(1,321 real changes made)
(1,527 real changes made)
(1,622 real changes made)
(1,636 real changes made)
(1,782 real changes made)
(1,767 real changes made)
(1,737 real changes made)

{com}. drop if Attribute=="Status";
{txt}(4,605 observations deleted)

{com}. destring _*, replace;
{txt}_2003 already numeric; no {res}replace
{txt}_2004 already numeric; no {res}replace
{txt}_2005 already numeric; no {res}replace
{txt}_2006 already numeric; no {res}replace
{txt}_2007 already numeric; no {res}replace
{txt}_2008 already numeric; no {res}replace
{txt}_2009 already numeric; no {res}replace
{txt}_2010 already numeric; no {res}replace
{txt}_2011: all characters numeric; {res}replaced {txt}as {res}double
{txt}(27499 missing values generated)
{res}{txt}_2012: all characters numeric; {res}replaced {txt}as {res}double
{txt}(27326 missing values generated)
{res}{txt}_2013 already numeric; no {res}replace
{txt}_2014: all characters numeric; {res}replaced {txt}as {res}double
{txt}(22775 missing values generated)
{res}{txt}_2015: all characters numeric; {res}replaced {txt}as {res}double
{txt}(19166 missing values generated)
{res}{txt}_2016: all characters numeric; {res}replaced {txt}as {res}double
{txt}(19909 missing values generated)
{res}{txt}_2017: all characters numeric; {res}replaced {txt}as {res}double
{txt}(18678 missing values generated)
{res}{txt}_2018: all characters numeric; {res}replaced {txt}as {res}double
{txt}(19028 missing values generated)
{res}{txt}_2019: all characters numeric; {res}replaced {txt}as {res}double
{txt}(19678 missing values generated)
{res}{txt}_2020 already numeric; no {res}replace
{txt}
{com}. drop Attribute;
{txt}
{com}. /* Reshape in long format */
> 
> reshape long _, i(Country_Code Indicator_Code Counterpart_Country_Code) j(year);
{txt}(j = 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020)

Data{col 36}Wide{col 43}->{col 48}Long
{hline 77}
Number of observations     {res}      58,216   {txt}->   {res}1,047,888   
{txt}Number of variables        {res}          21   {txt}->   {res}5           
{txt}j variable (18 values)                    ->   {res}year
{txt}xij variables:
                  {res}_2003 _2004 ... _2020   {txt}->   {res}_
{txt}{hline 77}

{com}. /* Rename variables */
> 
> rename _ amount;
{res}{txt}
{com}. /* Drop missing data */
> 
> drop if missing(amount);
{txt}(470,276 observations deleted)

{com}. /* Merge investor country code */
> 
> replace Country_Code = 354 if Country_Code==355;
{txt}(5,170 real changes made)

{com}.         /* Curacao and Sint Maarten -> Curacao */
> 
> merge m:1 Country_Code using Countries,
>         keepusing(country Iofc)
>         nogen keep(master match);
{res}
{txt}{col 5}Result{col 33}Number of obs
{col 5}{hline 41}
{col 5}Not matched{col 30}{res}           9,083
{txt}{col 9}from master{col 30}{res}           9,083{txt}  
{col 9}from using{col 30}{res}               0{txt}  

{col 5}Matched{col 30}{res}         568,529{txt}  
{col 5}{hline 41}

{com}. replace country = "_CR" if Country_Code==93;
{txt}(9,083 real changes made)

{com}.         /* 93 = International Organizations and SEFER */
> replace Iofc = 0 if Country_Code==93;
{txt}(9,083 real changes made)

{com}. drop Country_Code;
{txt}
{com}. /* Merge issuer country code */
> 
> merge m:1 Counterpart_Country_Code using `Countries',
>         nogen keep(master match);
{res}
{txt}{col 5}Result{col 33}Number of obs
{col 5}{hline 41}
{col 5}Not matched{col 30}{res}           6,244
{txt}{col 9}from master{col 30}{res}           6,244{txt}  
{col 9}from using{col 30}{res}               0{txt}  

{col 5}Matched{col 30}{res}         571,368{txt}  
{col 5}{hline 41}

{com}. replace Counterpart = "_OC" if inlist(Counterpart_Country_Code,91,983);
{txt}(6,244 real changes made)

{com}.         /* 91 = International Organizations, 983 = Not Specified */
> 
> drop Counterpart_Country_Code;
{txt}
{com}. /* Reassign territories to countries */
> 
> foreach var of varlist country Counterpart {c -(};
{txt}  2{com}.         replace `var' = "GBR" if `var'=="GIB";
{txt}  3{com}.         /* Gibraltar -> UK */
>         replace `var' = "SRB" if `var'=="XKX";
{txt}  4{com}.         /* Kosovo -> Serbia */
>         replace `var' = "NLD" if `var'=="CUW";
{txt}  5{com}.         /* Curacao -> Netherlands */
> {c )-};
{txt}(2,732 real changes made)
(7,983 real changes made)
(5,170 real changes made)
(2,222 real changes made)
(1,341 real changes made)
(1,716 real changes made)

{com}. drop if country==Counterpart;
{txt}(212 observations deleted)

{com}. /* Construct variables */
> 
> gen byte type = 1 if regexm(Indicator_Code,"^I_A_D_S");
{txt}(397,125 missing values generated)

{com}. replace type = 2 if regexm(Indicator_Code,"^I_A_D_L");
{txt}(199,045 real changes made)

{com}. replace type = 3 if regexm(Indicator_Code,"^I_A_E_T");
{txt}(198,080 real changes made)

{com}. drop Indicator_Code;
{txt}
{com}. /* Aggregate across territories and other countries */
> 
> collapse (sum) amount, by(year country Counterpart type Iofc) fast;
{res}{txt}
{com}. /* Convert to US$ billion and truncate short positions */
> 
> replace amount = max(amount/1e9,0);
{txt}(184,604 real changes made)

{com}. /*
> /* Replace amounts below reportable threshold */
> 
> gen Lamount1 = ln(amount);
> gen Lamount2 = ln(amount);
> replace Lamount2 = ln($small) if amount==0;
> 
> qui forval y = $year_min/$year_max {c -(};
>         forval j = 1/3 {c -(};
>                 xi: intreg Lamount1 Lamount2 i.country i.Counterpart if year==`y' & type==`j', robust;
> 
>                 predict ystar if year==`y' & type==`j', ystar(Lamount1,Lamount2);
> 
>                 replace amount = exp(ystar) if year==`y' & type==`j' & amount==0;
> 
>                 drop ystar;
>         {c )-};
> {c )-};
> 
> xi;
> 
> drop Lamount*;
> */
> /* Merge share in common equity */
> 
> merge m:1 year country Counterpart type using Restatement_equity,
>         keep(master match);
{res}
{txt}{col 5}Result{col 33}Number of obs
{col 5}{hline 41}
{col 5}Not matched{col 30}{res}         540,647
{txt}{col 9}from master{col 30}{res}         540,647{txt}  (_merge==1)
{col 9}from using{col 30}{res}               0{txt}  (_merge==2)

{col 5}Matched{col 30}{res}          21,531{txt}  (_merge==3)
{col 5}{hline 41}

{com}. /* Assume common equity if breakdown unknown */
> 
> replace Scommon = 1 if type==3 & _merge==1;
{txt}(171,268 real changes made)

{com}. /* Split into common equity and fund shares */
> 
> expand 2 if _merge==3, gen(duplicate);
{txt}(21,531 observations created)

{com}. replace type = type+duplicate;
{txt}(21,531 real changes made)

{com}. replace amount = amount*Scommon if type==3;
{txt}(11,022 real changes made)

{com}.                 /* Common equity */
> replace amount = amount*(1-Scommon) if type==4;
{txt}(16,290 real changes made)

{com}.         /* Fund shares */
> 
> drop Scommon _merge duplicate;
{txt}
{com}. /* Drop zeros */
> 
> drop if amount==0;
{txt}(389,467 observations deleted)

{com}. /* Label variables */
> 
> order year country Counterpart type Iofc;
{txt}
{com}. label var year          "Year";
{txt}
{com}. label var type          "Asset type";
{txt}
{com}. label var amount        "Investment amount (US$ billion)";
{txt}
{com}. label define type_label
>         1 "Short-term debt"
>         2 "Long-term debt"
>         3 "Equity"
>         4 "Fund shares", replace;
{txt}
{com}. label val type type_label;
{txt}
{com}. /* Save data */
> 
> sort year country Counterpart type;
{txt}
{com}. save IMF_CPIS, replace;
{txt}{p 0 4 2}
file {bf}
IMF_CPIS.dta{rm}
saved
{p_end}

{com}. /* Step 3: Construct IMF CPIS currency composition */
> 
> /* Load data */
> 
> u Country_Code Indicator_Code Attribute
>         _$year_min-_$year_max
>         if regexm(Indicator_Code,"^I_A_(D_L|D_S|E_T)_(USD|CHF|EUR|GBP|JPY|USD)")
>         & Attribute=="Value"
>         using "`directory'/CPIS_03-10-2022", clear;
{txt}
{com}. drop Attribute;
{txt}
{com}. /* Fix variables */
> 
> destring _*, replace;
{txt}_2003 already numeric; no {res}replace
{txt}_2004 already numeric; no {res}replace
{txt}_2005 already numeric; no {res}replace
{txt}_2006 already numeric; no {res}replace
{txt}_2007 already numeric; no {res}replace
{txt}_2008 already numeric; no {res}replace
{txt}_2009 already numeric; no {res}replace
{txt}_2010 already numeric; no {res}replace
{txt}_2011: all characters numeric; {res}replaced {txt}as {res}double
{txt}(359 missing values generated)
{res}{txt}_2012: all characters numeric; {res}replaced {txt}as {res}double
{txt}(340 missing values generated)
{res}{txt}_2013 already numeric; no {res}replace
{txt}_2014: all characters numeric; {res}replaced {txt}as {res}double
{txt}(226 missing values generated)
{res}{txt}_2015: all characters numeric; {res}replaced {txt}as {res}double
{txt}(195 missing values generated)
{res}{txt}_2016: all characters numeric; {res}replaced {txt}as {res}double
{txt}(149 missing values generated)
{res}{txt}_2017: all characters numeric; {res}replaced {txt}as {res}double
{txt}(132 missing values generated)
{res}{txt}_2018: all characters numeric; {res}replaced {txt}as {res}double
{txt}(135 missing values generated)
{res}{txt}_2019: all characters numeric; {res}replaced {txt}as {res}double
{txt}(114 missing values generated)
{res}{txt}_2020 already numeric; no {res}replace
{txt}
{com}. /* Reshape in long format */
> 
> reshape long _, i(Country_Code Indicator_Code) j(year);
{txt}(j = 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020)

Data{col 36}Wide{col 43}->{col 48}Long
{hline 77}
Number of observations     {res}         872   {txt}->   {res}15,696      
{txt}Number of variables        {res}          20   {txt}->   {res}4           
{txt}j variable (18 values)                    ->   {res}year
{txt}xij variables:
                  {res}_2003 _2004 ... _2020   {txt}->   {res}_
{txt}{hline 77}

{com}. /* Rename variables */
> 
> rename _ Tcurrency;
{res}{txt}
{com}. /* Drop missing data */
> 
> drop if missing(Tcurrency);
{txt}(5,883 observations deleted)

{com}. /* Merge country code */
> 
> merge m:1 Country_Code using Countries,
>         keepusing(country)
>         nogen keep(master match);
{res}
{txt}{col 5}Result{col 33}Number of obs
{col 5}{hline 41}
{col 5}Not matched{col 30}{res}               0
{txt}{col 5}Matched{col 30}{res}           9,813{txt}  
{col 5}{hline 41}

{com}. drop Country_Code;
{txt}
{com}. /* Reassign territories to countries */
> 
> replace country = "GBR" if country=="GIB";
{txt}(0 real changes made)

{com}.         /* Gibraltar -> UK */
> replace country = "SRB" if country=="XKX";
{txt}(165 real changes made)

{com}.         /* Kosovo -> Serbia */
> replace country = "NLD" if country=="CUW";
{txt}(0 real changes made)

{com}.         /* Curacao -> Netherlands */
> 
> /* Construct variables */
> 
> gen byte type = 1 if regexm(Indicator_Code,"^I_A_D_S");
{txt}(6,734 missing values generated)

{com}. replace type = 2 if regexm(Indicator_Code,"^I_A_D_L");
{txt}(3,410 real changes made)

{com}. replace type = 3 if regexm(Indicator_Code,"^I_A_E_T");
{txt}(3,324 real changes made)

{com}. gen currency = regexs(2) if regexm(Indicator_Code,"^I_A_(D_L|D_S|E_T)_(CHF|EUR|GBP|JPY|USD)");
{txt}
{com}. replace currency = "CHE" if currency=="CHF";
{txt}(1,755 real changes made)

{com}. replace currency = "GBR" if currency=="GBP";
{txt}(1,919 real changes made)

{com}. replace currency = "JPN" if currency=="JPY";
{txt}(1,805 real changes made)

{com}. replace currency = "USA" if currency=="USD";
{txt}(2,273 real changes made)

{com}. drop Indicator_Code;
{txt}
{com}. /* Aggregate across territories */
> 
> collapse (sum) Tcurrency, by(year country type currency) fast;
{res}{txt}
{com}. /* Convert to US$ billion and truncate short positions */
> 
> replace Tcurrency = max(Tcurrency/1e9,0);
{txt}(7,997 real changes made)

{com}. /* Drop zeros */
> 
> drop if Tcurrency==0;
{txt}(1,826 observations deleted)

{com}. /* Label variables */
> 
> order year country type currency;
{txt}
{com}. label var year          "Year";
{txt}
{com}. label var type          "Asset type";
{txt}
{com}. label var currency      "Currency";
{txt}
{com}. label var Tcurrency     "Investment amount (US$ billion)";
{txt}
{com}. label define type_label
>         1 "Short-term debt"
>         2 "Long-term debt"
>         3 "Equity"
>         4 "Fund shares";
{txt}
{com}. label val type type_label;
{txt}
{com}. /* Save data */
> 
> sort year country type currency;
{txt}
{com}. save IMF_CPIS_currency, replace;
{txt}{p 0 4 2}
file {bf}
IMF_CPIS_currency.dta{rm}
saved
{p_end}

{com}. log close;
      {txt}name:  {res}<unnamed>
       {txt}log:  {res}C:\Users\myogo\Princeton Dropbox\Motohiro Yogo\Koijen Yogo - International\JPE\Replication\Code\1 Data\IMF_CPIS.smcl
  {txt}log type:  {res}smcl
 {txt}closed on:  {res}31 Oct 2025, 09:19:09
{txt}{.-}
{smcl}
{txt}{sf}{ul off}